<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor
        :itemList="[
          { name: 'box-base', title: `${cpBusTitle}信息` },
          { name: 'box-file', title: `${cpBusTitle}附件` },
          { name: 'box-dtl', title: `${cpBusTitle}详情` },
        ]"
      />
    </div>
    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" :rules="propRules" ref="editForm" :disabled="propReadonly">
        <template #content>
          <div id="box-base" class="box-title">{{cpBusTitle}}信息</div>
          <customFormItem
            label="采购单号"
            name="poKey"
            :parent="_self"
            :form="form"
            :readonly="true"
            :showSelectIcon="true"
            @focus="$refs.tableDrawerPo.show()"
            :disabled="!propParent.cpFormNew"
          />

          <customFormItem :label="cpBusTitle + '名称'" name="busTitle" :parent="_self" :form="form"/>

          <customFormItem
            label="来源科室"
            name="deptNameFrom"
            :parent="_self"
            :form="form"
            :readonly="true"
            :showSelectIcon="true"
            @focus="utilSelect.dept(deptNameFromChange)"
            :disabled="!propParent.cpFormNew"
          />
          <customFormItem label="报价单号" name="busKey" :parent="_self" :form="form" disabled/>
          <customFormItem label="申请日期" name="busDate" :parent="_self" :form="form"
                          :type="constCommon.formItemType.datePicker.name"  disabled/>
          <customFormItem label="申请人" name="busUser" :parent="_self" :form="form"  disabled/>
          <customFormItem label="申请人电话" name="applyPhone" :parent="_self" :form="form"  />

          <customFormItem label="备注" name="remark" :parent="_self" :form="form"/>

<!--          <customFormItem label="业务类型" name="busType" :parent="_self" :form="form"
              :type="constCommon.formItemType.selectDict.name"
              :config="{dictType: constCommon.dictType.deviceBusType}"
              disabled
          />-->

          <div id="box-file" class="box-title">{{cpBusTitle}}附件</div>
          <customFormItem
            label=""
            name="workFile"
            :parent="_self"
            :form="form"
            :type="constCommon.formItemType.slot.name"
            fullWidth
          >
            <template #content>
              <customUpload
                :data="{ fileRefId: form.id+ 'apply', fileRefTable: 'device_work_item', fileSavePath: 'device_work_item'}"
                :hide="propParent.cpFormNew"
                hideMsg="请先保存主单据后再上传"
                :max="20"
                :disabled="propReadonly"
                multiple
              />
            </template>
          </customFormItem>
        </template>
      </customForm>


      <div id="box-dtl" class="box-title">{{cpBusTitle}}详情</div>
      <data-view ref= "childDataView"
                 :propHideFlowSelect = "true"
                 propViewKey="device_work_item_offer"
                 :propFlowKey="propParent.propFlowKey"
                 :propInitData="form"
                 :propParentPage="_self"
                 :propParentData="propParent.viewFormData"
                 :propAutoFetchData="false"
                 :propTableHide="propParent.cpFormNew"
                 propTableHideMsg="请先保存主单据"
                 class="data-view-inlinebtn"
                 @afterTableDataChange="afterTableDataChange"
                 propViewFormTitle="报价明细"
                 propViewForm="device/device_work/po_offer/po_offer_item.vue"
                 :propIsTdesign="true"
                 :propOnlyQueryAll="propReadonly"
                 :propBeforeClick="propBeforeClickDtl"
      />

    </div>

    <!--采购明细选择-->
    <tableDrawer ref="tableDrawerPoDtl" refName="tableDrawerPoDtl" viewKey="device_work_item_select" attach="body"
      loadForce
       :queryWrapper="$newWrapper()"
       :propUserParam="{busType: constCommon.dictType.deviceBusTypePoOffer}"
    />

    <!--采购选择-->
    <tableDrawer ref="tableDrawerPo" refName="tableDrawerPo" viewKey="device_work" attach="body" @confirm="tableDrawerPoSelect"
      loadForce
       :queryWrapper="cpQueryWrapperPo"
       :propUserParam="{busType: constCommon.dictType.deviceBusTypePo}"
    />
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'
import customUpload from "@/components/custom/form/customUpload.vue";

export default {
  desc: '设备-采购-详情',
  data() {
    return {
      form: {},
    }
  },
  computed: {
    // 业务类型
    cpBusType() {
      return this.propParam && this.propParam.busType ? this.propParam.busType : -1
    },
    // 业务标题
    cpBusTitle() {
      return this.utils.getDictLabel(this.cpBusType, this.constCommon.dictType.deviceBusType, null, false)
    },
    // 采购单选择条件
    cpQueryWrapperPo() {
      let query = this.$newWrapper()
      query.eq('busType', this.constCommon.dictType.deviceBusTypePo)
      query.eq('flowStatus', 'end')
      return query
    }
  },
  created() {
    let that = this
    this.$emit('event-register', {
      //表单校验
      validate: (callback) => {
        return this.$refs.editForm.validate(callback)
      },

      //保存前操作
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        if (this.strings.isNotBlank(this.form.deptNameFrom) && this.strings.isNotBlank(this.form.deptNameTo) && this.form.deptNameFrom == this.form.deptNameTo) {
          this.utilMessage.warning('来源科室与目标科室一样，请调整')
          return false
        }
        return true
      },

      //数据加载后
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        if (this.propParent.cpFormNew) {
          this.$set(this.form, 'busUser', this.utils.getCurrentUser().name)
          this.$set(this.form, 'deptIdFrom', this.utils.getCurrentUser().deptId)
          this.$set(this.form, 'deptNameFrom', this.utils.getCurrentUser().deptName)
          this.$set(this.form, 'busDate', new Date().format(this.dates.defaultDatePattern))
          this.$set(this.form, 'busType', this.cpBusType)
          this.$set(this.form, 'busKey', '自动获取')
        }
        this.loadDtl()
      },

      //数据保存后
      afterSave: () => {
        that.loadDtl()
      },
      beforeDelete: () => {
        return true
      },

      //数据删除后
      afterDelete: () => {
      },

      //流程提交前
      beforeFlow: (flowButtonKey) => {
        return true
      },

      //流程提交后
      afterFlow: (valueTo, thatParam, buttonKey) => {
        if (valueTo =='end'){
          that.loadDtl()
        }
      },

      //表单关闭前
      beforeClose: () => {
        return true
      }
    })
  },
  props: {
    //上级对象
    propParent: Object,
    //上级初始化数据
    propInitData: Object,
    //主键字段
    propPrimaryKey: String,
    //数据字典
    propDictionaries: Object,
    //当前操作行
    propCurrentRow: Object,
    //表单数据
    propViewFormData: Object,
    //是否只读
    propReadonly: Boolean,
    //操作状态
    propStatus: String,
    //表单规则
    propRules: Object,
    propParam: Object,
  },
  components: {
    customUpload,
    customAnchor
  },
  methods: {

    // 加载明细
    loadDtl() {
      let queryWrapper = this.$newWrapper()
      if (this.form.id) {
        queryWrapper.eq('workId', this.form.id)
      } else {
        queryWrapper.eq('workId', -1)
      }
      this.$refs.childDataView.setQueryWrapper(queryWrapper)
    },

    // 明细 新增、删除、修改后调用
    afterTableDataChange() {
      this.utils.refreshRefs(this)
    },
    // 采购单选择
    tableDrawerPoSelect(item) {
      this.$set(this.form, 'poId', item.id)
      this.$set(this.form, 'poKey', item.busKey)
      this.$set(this.form, 'busTitle', item.busTitle)
    },
    // 来源科室选择
    deptNameFromChange(item) {
      this.$set(this.form, 'deptIdFrom', item.id)
      this.$set(this.form, 'deptNameFrom', item.name)
    },
    // 选择完采购明细
    tableDrawerPoDtlConfirm(rows) {
      //转换属性
      rows.forEach(ob => {
        ob.workId = this.form.id
        ob.workItemId = ob.id
        ob.id = null
      })

      //批量添加
      this.utilVue.loadShow(true)
      this.$ajax.post('/device/work_item/saveBatch', rows).then(resp => {
        this.utilVue.loadHide()
        if (resp.code == 0) {
          this.utilMessage.success(resp.msg)
          this.loadDtl()
        }
      })
    },
    // 选择完采购明细
    propBeforeClickDtl(button, formData , rowData, that) {
      if (button.key == 'vb-add') {
        this.$refs.tableDrawerPoDtl.show(this.tableDrawerPoDtlConfirm, true)
        return false
      }
      return true
    },
  }
}
</script>
